#include "TcpSocket.h"

#ifdef __linux__
bool TcpSocket::Create(TcpSocket::Desc desc)
{

    sock = socket(desc.af, desc.type, desc.protocol); // 创建一个空的socket
    if (-1 == sock)
    {
        LOGI("W invalied sock", "");
        return false;
    }

    struct sockaddr_in addr;
    addr.sin_family = desc.sinFamily;
    addr.sin_port = htons(desc.port);
    addr.sin_addr.s_addr = inet_addr(desc.host);

    if (-1 == connect(sock, (struct sockaddr *)&addr, sizeof(addr)))
    {
        LOGI("Failed to connect the socket %s %d", desc.host, desc.port);
        return false;
    }
    LOGI("create tcpClient successfully", "");

    return true;
}

void TcpSocket::Close()
{
    close(sock); // 关闭
}
#endif